<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="出款银行" prop="tableName">
        <el-select v-model="queryParams.tableName" placeholder="请选择出款银行" clearable>
          <el-option label="蓝海银行" value="ecs_pay_ease" />
          <el-option label="蓝海/苏商/厦门" value="ecs_yee_pay" />
          <el-option label="苏商/华通" value="ecs_baofu_merchant" />
        </el-select>
      </el-form-item>
      <el-form-item label="收款方姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入收款方姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="提现卡类型" prop="transferCardType">
        <el-select v-model="queryParams.transferCardType" placeholder="请选择提现卡类型" clearable>
          <el-option label="个人" :value="1" />
          <el-option label="对公" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="支行名称" prop="branchBankName">
        <el-input v-model="queryParams.branchBankName" placeholder="请输入支行名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ecs:externalTransferAccount:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ecs:externalTransferAccount:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ecs:externalTransferAccount:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ecs:externalTransferAccount:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="externalTransferAccountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="出款银行" align="center" prop="tableName">
        <template slot-scope="scope">
          <span v-if="scope.row.tableName === 'ecs_pay_ease'">蓝海银行</span>
          <span v-else-if="scope.row.tableName === 'ecs_yee_pay'">蓝海/苏商/厦门</span>
          <span v-else-if="scope.row.tableName === 'ecs_din_pay'">智付锡商</span>
          <span v-else-if="scope.row.tableName === 'ecs_baofu_merchant'">苏商/华通</span>
        </template>
      </el-table-column>
      <el-table-column label="收款方姓名" align="center" prop="name" />
      <el-table-column label="提现卡类型" align="center" prop="transferCardType">
        <template slot-scope="scope">
          <span v-if="scope.row.transferCardType === 1">个人</span>
          <span v-if="scope.row.transferCardType === 2">对公</span>
        </template>
      </el-table-column>
      <el-table-column label="银行名称" align="center" prop="bankName" />
      <el-table-column label="支行名称" align="center" prop="branchBankName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ecs:externalTransferAccount:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ecs:externalTransferAccount:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改接收方信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="出款银行" maxlength="64" prop="tableName">
          <el-select @change="form.bankName = null; bankName = [];" v-model="form.tableName" placeholder="请选择出款银行"
            clearable>
            <el-option label="蓝海银行" value="ecs_pay_ease" />
            <el-option label="蓝海/苏商/厦门" value="ecs_yee_pay" />
            <el-option label="苏商/华通" value="ecs_baofu_merchant" />
          </el-select>
        </el-form-item>
        <el-form-item label="收款方姓名" maxlength="64" prop="name">
          <el-input v-model="form.name" placeholder="请输入收款方姓名" />
        </el-form-item>
        <el-form-item label="提现卡类型" prop="transferCardType">
          <el-select v-model="form.transferCardType" placeholder="请选择提现卡类型" clearable>
            <el-option label="个人" :value="1" />
            <el-option label="对公" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="银行名称" maxlength="64" prop="bankName">
          <el-select filterable remote v-model="form.bankName" placeholder="请输入银行名称" :remote-method="remoteMethod"
            :loading="bankNameLoading">
            <el-option v-for="item in bankName" :key="item.name" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <template v-if="form.tableName === 'ecs_baofu_merchant'">
          <el-form-item label="开户行省" prop="province"
            :rules="[{ required: true, message: '请输入开户行所在省份', trigger: 'blur' }]">
            <name-input v-model="form.province" placeholder="请输入开户行省，如: 广东省" />
          </el-form-item>
          <el-form-item label="开户行市" prop="city" :rules="[{ required: true, message: '请输入开户行所在城市', trigger: 'blur' }]">
            <name-input v-model="form.city" placeholder="请输入开户行市，如: 广州市" />
          </el-form-item>
        </template>
        <el-form-item label="支行名称" maxlength="64" prop="branchBankName">
          <el-input v-if="form.tableName !== 'ecs_din_pay'" v-model="form.branchBankName" placeholder="请输入支行名称" />
          <el-select v-else filterable remote v-model="form.branchBankName" placeholder="请输入支行名称"
            :remote-method="remoteMethods" :loading="bankNameLoading">
            <el-option v-for="item in branchBankName" :key="item.branchBankName" :label="item.branchBankName"
              :value="item.branchBankName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="银行卡号" maxlength="64" prop="bankCardNum">
          <code-input v-model="form.bankCardNum" placeholder="请输入银行卡号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExternalTransferAccount, getExternalTransferAccount, delExternalTransferAccount, addExternalTransferAccount, updateExternalTransferAccount } from "@/api/ecs/externalTransferAccount";
import { payEaseBank } from '@/api/ecs/payEase'
import { listYeePayBank } from '@/api/ecs/yeePayBank'
import { listDinPayBank } from '@/api/ecs/dinPayBank'

export default {
  name: "externalTransferAccount",
  data() {
    return {
      // 遮罩层
      loading: true,
      bankNameLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      bankName: [],
      branchBankName: [],
      // 接收方信息表格数据
      externalTransferAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        transferName: null,
        transferBankNo: null,
        transferCardType: null,
        transferMobile: null,
        transferBankCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "收款方姓名不能为空", trigger: "blur" }
        ],
        tableName: [
          { required: true, message: "请选择出款银行", trigger: "blur" }
        ],
        transferCardType: [
          { required: true, message: "请选择提现卡类型", trigger: "change" }
        ],
        bankCardNum: [
          { required: true, message: "银行卡号不能为空", trigger: "blur" }
        ],
        bankName: [
          { required: true, message: "银行不能为空", trigger: "blur" }
        ],
        branchBankName: [
          { required: true, message: "支行名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询接收方信息列表 */
    getList() {
      this.loading = true;
      listExternalTransferAccount(this.queryParams).then(response => {
        this.externalTransferAccountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.bankName = []
      this.branchBankName = []
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        tableName: "ecs_pay_ease",
        transferCardType: null,
        bankCardNum: null,
        bankName: null,
        branchBankName: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加接收方信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExternalTransferAccount(id).then(response => {
        const additionalParameters = JSON.parse(response.data.additionalParameters ?? '{}')
        this.form = { ...response.data, ...additionalParameters };
        this.open = true;
        this.title = "修改接收方信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.additionalParameters = JSON.stringify({
          province: this.form.province,
          city: this.form.city,
        })
        if (valid) {
          if (this.form.id != null) {
            updateExternalTransferAccount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExternalTransferAccount(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除接收方信息编号为"' + ids + '"的数据项？').then(function () {
        return delExternalTransferAccount(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/externalTransferAccount/export', {
        ...this.queryParams
      }, `externalTransferAccount_${new Date().getTime()}.xlsx`)
    },
    remoteMethod(query) {
      if (query !== '' && query !== null) {
        this.bankNameLoading = true
        if (this.form.tableName === "ecs_pay_ease") {
          payEaseBank({ name: query }).then(res => {
            this.bankName = res.data
            this.bankNameLoading = false
          }).catch(() => {
            this.bankNameLoading = true
          })
        } else if (this.form.tableName === "ecs_yee_pay") {
          listYeePayBank({ name: query }).then(res => {
            this.bankName = res.data
            this.bankNameLoading = false
          }).catch(() => {
            this.bankNameLoading = true
          })
        } else {
          listDinPayBank({ name: query, key: "name" }).then(res => {
            this.bankName = res.data
            this.bankNameLoading = false
          }).catch(() => {
            this.bankNameLoading = true
          })
        }

      } else {
        this.options = [];
      }
    },
    remoteMethods(query) {
      if (query !== '' && query !== null) {
        this.bankNameLoading = true
        listDinPayBank({ branchBankName: query, name: this.form.bankName, key: "branchBankName" }).then(res => {
          this.branchBankName = res.data
          this.bankNameLoading = false
        }).catch(() => {
          this.bankNameLoading = true
        })
      }
    },
  }
};
</script>
